home *** CD-ROM | disk | FTP | other *** search
- /******************************************************/
- /* */
- /* CBrowserDoc.c */
- /* */
- /* Written in Think C version 4.0.2 */
- /* Based on CStarterDoc.c */
- /* */
- /* Allen Stenger January 1991 */
- /* */
- /******************************************************/
-
- #define DOTIMING 0
- /* do timing measurements if true */
-
- #include <Global.h>
- #include <Commands.h>
- #include <CApplication.h>
- #include <CBartender.h>
- #include <CDataFile.h>
- #include <CDecorator.h>
- #include <CDesktop.h>
- #include <CError.h>
- #include <CPanorama.h>
- #include <CScrollPane.h>
-
- #include "CBrowserDoc.h"
- #include "CBrowserPane.h"
- #include "Browser.h"
- #include "Search.h"
- #include "LineCount.h"
-
- extern CApplication *gApplication;
- extern CBartender *gBartender;
- extern CDecorator *gDecorator;
- extern CDesktop *gDesktop;
- extern CBureaucrat *gGopher;
- extern OSType gSignature;
- extern CError *gError;
-
- void CBrowserDoc::IBrowserDoc(
- CApplication *aSupervisor,
- Boolean printable)
- {
- CDocument::IDocument(aSupervisor, printable);
- itsDataH = NULL;
- itsLineStartsH = NULL;
- itsLineCt = 0;
- itsFindOffset = -1;
- }
-
- void CBrowserDoc::Dispose()
- {
- if (itsDataH != NULL) DisposHandle(itsDataH);
- if (itsLineStartsH != NULL)
- DisposHandle(itsLineStartsH);
- if (itsMainPane != NULL) itsMainPane->Dispose();
- inherited::Dispose();
- }
-
- void CBrowserDoc::FindIt(long offset)
- {
- Point scrollToPoint;
- /* point in panorama coordinates to scroll to */
- int i; /* loop control */
-
- #if DOTIMING
- long startTime, endTime;
- float elapsedTime;
- /* time to search and scroll to right line (secs) */
-
- long startLineTime, endLineTime;
- float elapsedLineTime;
- /* time to find right line given the offset (secs)*/
- #endif
-
- #if DOTIMING
- startTime = TickCount();
- #endif
-
- if ( GoFind(itsDataH, &offset) ) {
- #if DOTIMING
- startLineTime = TickCount();
- #endif
- for (i=1;
- (*itsLineStartsH)[i]<=offset;
- i++)
- ;
- #if DOTIMING
- endLineTime = TickCount();
- elapsedLineTime = (endLineTime - startLineTime)
- / 60.0;
- #endif
- scrollToPoint.v = i - 1;
- scrollToPoint.h = 0;
- ((CBrowserPane *)itsMainPane)->
- ScrollTo(scrollToPoint, TRUE);
- itsFindOffset = offset;
- }
- else SysBeep(1); /* not found */
-
- #if DOTIMING
- endTime = TickCount();
- elapsedTime = (endTime - startTime) / 60.0;
- #endif
- }
-
- void CBrowserDoc::DoCommand(long theCommand)
- {
- Boolean aSearchStringGiven;
- /* Find dialog got string */
-
- switch (theCommand) {
-
- case cmdFind:
- aSearchStringGiven = GetSearchString();
- itsWindow->Update();
- /* update window to correct damage */
- /* from Find dialog */
- if (aSearchStringGiven) {
- itsFindOffset = -1;
- FindIt( itsFindOffset + 1 );
- }
- break;
-
- case cmdFindAgain:
- FindIt( itsFindOffset + 1 );
- break;
-
- default: inherited::DoCommand(theCommand);
- break;
- }
- }
-
- void CBrowserDoc::UpdateMenus()
- {
- inherited::UpdateMenus();
-
- gBartender->EnableCmd(cmdFind);
- if ( HaveSearchString() )
- gBartender->EnableCmd(cmdFindAgain);
- gBartender->DisableCmd(cmdNew);
- gBartender->DisableCmd(cmdSaveAs);
- }
-
- void CBrowserDoc::OpenFile(SFReply *macSFReply)
- {
- CDataFile *theFile;
- Handle theData;
- Str63 theName;
- OSErr theError;
- short theLineCt; /* shadow variable */
- long **theLineStartsH; /* shadow variable */
-
- theFile = new(CDataFile);
- theFile->IDataFile();
- theFile->SFSpecify(macSFReply);
- itsFile = theFile;
- theError = theFile->Open(fsRdPerm);
- if (!gError->CheckOSError(theError)) {
- Dispose();
- return;
- }
- gApplication->RequestMemory(FALSE, TRUE);
- theFile->ReadAll(&theData);
-
- /* Reset canFail to FALSE for default */
- /* memory-error handling. */
- gApplication->RequestMemory(FALSE, FALSE);
- if (theData == NULL) {
- gError->CheckOSError(MemError());
- Dispose();
- return;
- }
- itsDataH = theData;
- /* save handle to data in instance variable */
- DoLineCount( itsDataH, &theLineCt, &theLineStartsH );
- itsLineCt = theLineCt;
- itsLineStartsH = theLineStartsH;
- BuildWindow(theData);
- itsFile->GetName(theName);
- itsWindow->SetTitle(theName);
- itsWindow->Select();
- theError = itsFile->Close();
- itsFile = NULL;
- }
-
- void CBrowserDoc::BuildWindow (Handle theData)
- {
- CScrollPane *theScrollPane;
- CBrowserPane *theMainPane;
-
- itsWindow = new(CWindow);
- itsWindow->IWindow(WINDBrowser, FALSE,
- gDesktop, this);
- theScrollPane = new(CScrollPane);
- theScrollPane->IScrollPane(itsWindow, this,
- 10, 10, 0, 0,
- sizELASTIC, sizELASTIC,
- TRUE, TRUE, TRUE);
- theScrollPane->FitToEnclFrame(TRUE, TRUE);
- theMainPane = new(CBrowserPane);
- theMainPane->IBrowserPane(theScrollPane, this,
- 0, 0, 0, 0,
- sizELASTIC, sizELASTIC,
- itsDataH, itsLineCt, itsLineStartsH);
- itsMainPane = theMainPane;
- itsGopher = theMainPane;
- theMainPane->FitToEnclosure(TRUE, TRUE);
- theScrollPane->InstallPanorama(theMainPane);
- gDecorator->PlaceNewWindow(itsWindow);
- itsWindow->Zoom(inZoomOut); /* open full-screen */
- }
-